import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '../views/login/login.vue'
import Register from '../views/register/register.vue'
import Home from '../views/home/home.vue'
import studentsList from '../views/home/studentsList.vue'
import addStudent from '../views/home/addStudent.vue'
import updateStudent from '../views/home/updateStudent.vue'
import classesList from '../views/home/classesList.vue'
import addClasses from '../views/home/addClasses.vue'
import subjectList from '../views/home/subjectList.vue'
import counter from '../views/home/others/counter.vue'
import api from '../http/api.js'
import store from '../store/index.js'
import axios from '../http/axios.js'

// import Error from '../views/404/Error.vue'
//路由懒加载
const Error = () =>
    import ('../views/404/Error.vue')

Vue.use(VueRouter)

const routes = [
    // 路由信息对象
    {
        path: '/home',
        name: 'Home',
        component: Home,
        beforeEnter: async(to, from, next) => {
            const token = localStorage.token;
            if (token) {
                const { data } = await api.users.getuserinfo();
                console.log(data);
                if (data.code) {
                    store.commit('SET_USERINFO', data.data);
                    next();
                }
            } else {
                alert('你还没有登录，请先登录');
                next('/login');
            }
        },
        children: [{
                path: 'studentsList',
                name: 'studentsList',
                component: studentsList,
                meta: {
                    title: '学生管理',
                    subtitle: '学生列表',
                    isKeepAlive: false
                }
            },
            {
                path: 'addStudent',
                name: 'addStudent',
                component: addStudent,
                meta: {
                    title: '学生管理',
                    subtitle: '新增学生',
                    isKeepAlive: true
                }
            },
            {
                path: 'updateStudent/:studentsId?',
                name: 'updateStudent',
                component: updateStudent,
                meta: {
                    title: '学生管理',
                    subtitle: '修改学生',
                    isKeepAlive: false
                }
            },
            {
                path: 'classesList',
                name: 'classesList',
                component: classesList,
                meta: {
                    title: '班级管理',
                    subtitle: '班级列表',
                    isKeepAlive: false
                }
            },
            {
                path: 'addClasses',
                name: 'addClasses',
                component: addClasses,
                meta: {
                    title: '班级管理',
                    subtitle: '新增班级',
                    isKeepAlive: true
                }
            },
            {
                path: 'subjectList',
                name: 'subjectList',
                component: subjectList,
                meta: {
                    title: '专业管理',
                    subtitle: '专业列表',
                    isKeepAlive: true
                }
            },
            {
                path: 'others/counter',
                name: 'counter',
                component: counter,
                meta: {
                    title: '其他功能',
                    subtitle: '计数',
                    isKeepAlive: true
                }
            },
        ]
    },
    {
        path: '/',
        redirect: '/login', //重定向
        // component: Login
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    {
        path: '/*',
        // component: Error,
        //路由懒加载
        component: Error
    },

    // {
    //   path: '/',
    //   name: 'Home',
    //   component: Home
    // },
    // {
    //   path: '/about',
    //   name: 'About',
    //   // route level code-splitting
    //   // this generates a separate chunk (about.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    // }
]

const router = new VueRouter({
    routes,
    //路由模式
    mode: 'history'
})

//全局守卫
router.beforeEach((to, from, next) => {
    if (to.path.includes('home')) {
        const token = localStorage.token;
        if (token) {
            next();
        } else {
            alert('你还没有登录，请先登录');
            next('/login');
        }
    } else {
        next();
    }
})



export default router